﻿// ===============================================================================
// Copyright(c) 2007-2020 西安交通信息投资营运有限公司 版权所有
// 作者:马星俊
// ===============================================================================

using System.ComponentModel;
using System.Threading.Tasks;
using DevExpress.Utils.Design;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList;
using XCI.Core;
using XCI.Sys.Filter;
using XCI.Sys.Service;
using XCI.Helper;

namespace XCI.Sys.Controls
{
    [System.ComponentModel.DesignerCategory("Code")]
    public class DeptUiTreeListLookUpEdit : TreeListLookUpEdit
    {
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (WinFormHelper.IsDesignMode()) return;

            this.Properties.ValueMember = "Id";
            this.Properties.DisplayMember = "Name";
            this.Properties.PopupWidthMode = PopupWidthMode.UseEditorWidth;
            var treelist = this.Properties.TreeList;
            if (treelist == null) return;
            treelist.Columns.AddRange(new[] {
                new DevExpress.XtraTreeList.Columns.TreeListColumn
                {
                    Caption = "名称",
                    FieldName = "NameCode",
                    Name = "treeColName",
                    Visible = true,
                    VisibleIndex = 0
                }});
            treelist.KeyFieldName = "Id";
            treelist.ParentFieldName = "ParentId";
            treelist.OptionsSelection.EnableAppearanceFocusedCell = false;
            treelist.OptionsView.FocusRectStyle = DevExpress.XtraTreeList.DrawFocusRectStyle.None;
            treelist.OptionsView.ShowColumns = false;
            treelist.OptionsView.ShowHorzLines = false;
            treelist.OptionsView.ShowIndicator = false;
            treelist.OptionsView.ShowVertLines = false;
            treelist.OptionsView.ShowFilterPanelMode = DevExpress.XtraTreeList.ShowFilterPanelMode.Never;
            treelist.OptionsView.ShowAutoFilterRow = true;
            treelist.OptionsMenu.EnableNodeMenu = false;
            treelist.OptionsFilter.FilterMode = FilterMode.EntireBranch;
            treelist.OptionsBehavior.AutoPopulateColumns = true;
            treelist.StateImageList = SysDeptService.Instance.GetDeptImageCollection();
            treelist.GetStateImage += (s, e) => { e.NodeImageIndex = 0; };
        }
    }

    [System.ComponentModel.DesignerCategory("Code")]
    public class DeptTreeListLookUpEdit : DeptUiTreeListLookUpEdit, IAsyncBindingData
    {
        [Category("XCI"), Description("自动加载数据"), DefaultValue(false)]
        [SmartTagProperty("AutoLoadData", "XCI", SmartTagActionType.RefreshBoundsAfterExecute)]
        public virtual bool AutoLoadData { get; set; } = false;

        protected override async void OnCreateControl()
        {
            base.OnCreateControl();
            if (WinFormHelper.IsDesignMode()) return;
            if (AutoLoadData)
            {
                await BindingDataCoreAsync();
            }
        }

        /// <summary>
        /// 异步绑定数据
        /// </summary>
        public async Task BindingDataAsync()
        {
            if (AutoLoadData) return;
            if (WinFormHelper.IsDesignMode()) return;
            await BindingDataCoreAsync();
        }

        /// <summary>
        /// 异步绑定数据
        /// </summary>
        private async Task BindingDataCoreAsync()
        {
            if (this.IsDisposing || this.IsDisposed || Properties.TreeList == null) return;
            var list = await SysDeptService.Instance.SelectListAsync(new SysDeptFilter { Status = true });
            Properties.DataSource = list;
        }
    }
}